﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace JieBooLaw.Common.Security
{
    public class Cryptographer
    {
        private static readonly RijndaelManaged AES = new RijndaelManaged();
        private static Encoding CODETYPE = new UTF8Encoding();
        private static readonly string KEYARRY = "C16D81FD0816578F623370F1C870DE03";
        private static readonly SHA256Managed SHA256 = new SHA256Managed();

        static Cryptographer()
        {
            AES.Mode = CipherMode.ECB;
            AES.Padding = PaddingMode.PKCS7;
        }

        private Cryptographer()
        {
        }

        public static bool CompareSHA256(byte[] inputvale, byte[] comparevalue)
        {
            byte[] buffer = SHA256.ComputeHash(inputvale);
            byte[] buffer2 = SHA256.ComputeHash(comparevalue);
            return (buffer == buffer2);
        }

        public static bool CompareSHA256(string inputvale, string comparevalue)
        {
            byte[] bytes = CODETYPE.GetBytes(inputvale);
            byte[] buffer2 = CODETYPE.GetBytes(comparevalue);
            return CompareSHA256(bytes, buffer2);
        }

        public static byte[] CreateSHA256Code(string inputvalue)
        {
            byte[] bytes = CODETYPE.GetBytes(inputvalue);
            return SHA256.ComputeHash(bytes);
        }

        public static string DecryptorAES(byte[] inputvalue, byte[] keyArray)
        {
            if ((inputvalue == null) || (inputvalue.Length < 1))
            {
                throw new ArgumentException("inputvalue can not be null");
            }
            if ((keyArray == null) || (keyArray.Length < 1))
            {
                throw new ArgumentException("keyArray value was wrong");
            }
            AES.Key = keyArray;
            byte[] bytes = AES.CreateDecryptor().TransformFinalBlock(inputvalue, 0, inputvalue.Length);
            return CODETYPE.GetString(bytes);
        }

        public static string DecryptorAESFromString(string inputvalue)
        {
            return DecryptorAES(Convert.FromBase64String(inputvalue), CODETYPE.GetBytes(KEYARRY));
        }

        public static byte[] EncryptorAES(string inputvalue, byte[] keyArray)
        {
            if (string.IsNullOrEmpty(inputvalue))
            {
                throw new ArgumentException("inputvalue can not be null");
            }
            if ((keyArray == null) || (keyArray.Length < 1))
            {
                throw new ArgumentException("keyArray value was wrong");
            }
            byte[] bytes = CODETYPE.GetBytes(inputvalue);
            AES.Key = keyArray;
            return AES.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);
        }

        public static string EncryptorAESToString(string inputvalue)
        {
            return Convert.ToBase64String(EncryptorAES(inputvalue, CODETYPE.GetBytes(KEYARRY)));
        }
    }
}
